<template>
  <div class="box">
    <el-button size="medium" @click="open()">{{ title }}
      <i :class="icon"></i>
    </el-button>
    <!-- 标签盒子 -->
    <div class="tagBox" v-if="icon !== 'el-icon-arrow-right'">
      <div v-for="(item, i) in tagData" :key="item.id" class="tag" :style="i === id ? 'color:#ec4141' : 'color:#000'"
        @click="changeTag(item, i)">
        {{ item.name }}
      </div>
    </div>
    <!-- 歌单 -->
    <div class="content">
      <div v-for="item in songListData" :key="item.id" class="contentBox">
        <el-card shadow="always" :body-style="{ padding: '0px' }">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.playCount >= 10000
                  ? (item.playCount / 10000).toFixed(0) + "万"
                  : item.playCount
            }}
          </div>
          <el-image :src="item.coverImgUrl" @click="getItem(item.id)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image src="https://s4.music.126.net/style/web2/img/default/default_album.jpg"
                @click="getItem(item.id)" lazy></el-image>
            </div>
          </el-image>
          <div style="padding: 14px">
            <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
              <div class="desc">
                {{ item.name }}
              </div>
            </el-tooltip>
          </div>
        </el-card>
      </div>
    </div>
    <div class="more" @click="load()" v-if="more === true">
      查看更多歌单 <i class="el-icon-arrow-down"></i>
    </div>
    <div class="loading" v-if="show === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex';
import debounce from "@/utils/debounce.js";
export default {
  computed: {
    ...mapState(['cookie', 'ifPlay']),
  },
  mounted() {
    this.getTag();
    this.getSongList();
  },
  data() {
    return {
      value: [],
      icon: 'el-icon-arrow-right',
      tagData: null,
      songListData: null,
      title: '全部歌单',
      show: false,
      more: false,
      limit: 50,
      offset: 0,
      id: null,
    };
  },
  beforeRouteLeave(to, from, next) {
    this.show = true
    next()
  },
  methods: {
    // 这里的逻辑是进入页面时先获取到所有的歌单,然后点击全部歌单按钮的时候可以选择歌单的分类
    // 获取歌单分类
    ...mapActions(['saveSongList', 'clickPlay']),
    getTag() {
      this.tagData = null;
      this.$api.playListTags().then((ret) => {
        this.tagData = ret.tags;
      });
    },
    // 获取歌单
    getSongList: debounce(function (e) {
      this.icon = 'el-icon-arrow-right';
      this.songListData = null;
      this.show = true;
      this.$api
        .topPlayList({ cat: e, limit: this.limit, offset: this.offset, cookie: localStorage.getItem('cookie') })
        .then((ret) => {
          this.more = ret.more;
          this.songListData = ret.playlists;
          this.show = false;
        });
    }, 1000),
    // 打开分类
    open() {
      if (this.icon === 'el-icon-arrow-right') {
        this.icon = 'el-icon-arrow-down';
      } else if (this.icon === 'el-icon-arrow-down') {
        this.icon = 'el-icon-arrow-right';
      }
    },
    
    // 跳转到歌单明细
    async getItem(e) {
      this.show = true;
      this.$api.getPlayList({ id: e, timestamp: this.$utils.timestamp(), cookie: localStorage.getItem('cookie') }).then((ret) => {
        if (ret.code === 200) {
          this.saveSongList({ songList: ret.playlist }); // 获取到的明细存入VUEX
          this.$router.push(`/songList?id=${e}`);
          this.clickPlay({ ifPlay: true });
        } else {
          this.$message.error('超时获取失败!!!');
        }
      });
    },
    // 更换标签
    changeTag(item, i) {
      this.title = item.name;
      this.id = i;
      this.getSongList(item.name)
    },
    // 查看更多
    load() {
      this.offset += 1;
      if (this.more === true) {
        this.offset += 1;
        this.limit = this.offset * 50;
        this.getSongList(this.title)
      }
    },
  },
};
</script>

<style scoped>
.box {
  position: relative;
  margin-bottom: 40px;
}

.el-button {
  margin-top: 20px;
  border-radius: 20px !important;
  margin-bottom: 15px;
  height: 40px;
}

.tagBox {
  position: absolute;
  width: 720px;
  border: 1px solid #dcdfe6;
  border-radius: 20px;
  display: flex;
  flex-wrap: wrap;
  padding-top: 20px;
  padding-bottom: 20px;
  padding-left: 30px;
  z-index: 11;
  background: #ffffff;
}

.tag {
  width: 80px;
  cursor: pointer;
  margin-bottom: 20px;
  margin-right: 20px;
  font-size: 16px;
  display: flex;
  justify-content: left;
}

.el-card {
  margin-bottom: 10px;
  cursor: pointer;
  width: 200px;
  position: relative;
}

.el-card .el-image {
  width: 200px;
  height: 200px;
}

.el-image {
  width: 100%;
}

.el-row {
  margin-bottom: 40px;
}

.content {
  display: flex;
  flex-wrap: wrap;
}

.contentBox {
  margin-right: 5px;
}

.count {
  position: absolute;
  z-index: 10;
  width: 100%;
  text-align: right;
  color: #fff;
  background-image: linear-gradient(#080808, #aca5a538);
}

.desc {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}

.more {
  cursor: pointer;
  text-align: center;
}
</style>
